//
//  ProgressImageView.swift
//  Weibo
//
//  Created by 孙明海 on 16/2/19.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit

class ProgressImageView: UIImageView {
    
    var progress: CGFloat = 0.5 {
        didSet {
            progressView.progress = progress
        }
    }
    
    /// 构造函数
    init() {
        super.init(frame: CGRectZero)
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置控件
    private func setUI() {
        addSubview(progressView)
        
        progressView.backgroundColor = UIColor.clearColor()
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
    }
    
    //MARK: - 懒加载控件
    private lazy var progressView: ProgressView = ProgressView()
}
/// 进度视图类
private class ProgressView: UIView {
    var progress: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    override func drawRect(rect: CGRect) {
        let center = CGPointMake(rect.size.width * 0.5, rect.height * 0.5)
        let radius = min(rect.width, rect.height) * 0.5
        let startA: CGFloat = -CGFloat(M_PI_2)
        let endA = startA + progress * CGFloat(M_PI * 2)
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startA, endAngle: endA, clockwise: true)
        path.addLineToPoint(center)
        path.closePath()
        UIColor(white: 1.0, alpha: 0.3).setFill()
        path.fill()
    }
}
